<!-- 设备管理 消防给水及消火栓系统 室内消火栓 消火栓箱-->
<template>
  <div class="app-container">
    <TopMenu v-if="showTopMenu" :data-list="topDataLabel" @tabsChange="tabsChange"/>
    <div class="main-info-container">
      <!-- 查询和其他操作 -->
      <div class="filter-container">
        <el-form ref="listQuery" :inline="true" :model="listQuery" class="form-bottom-line" label-width="94px">
          <el-form-item v-if="showTopMenu" label="选择单位:">
            <el-select v-model="listQuery.corp_id" clearable filterable allow-create default-first-option autocomplete="off" placeholder="请选择单位名称" @change="companyChange">
              <el-option v-for="item in companyList" :key="item.corp_id" :label="item.name" :value="item.corp_id" />
            </el-select>
          </el-form-item>
          <el-form-item label="建筑群名称:">
            <el-select v-model="listQuery.park_id" clearable autocomplete="off" placeholder="请选择建筑群名称" @change="parkChange">
              <el-option v-for="item in parksList" :key="item.park_id" :label="item.name" :value="item.park_id" />
            </el-select>
          </el-form-item>
          <el-form-item label="建筑名称:">
            <el-select v-model="listQuery.building_id" clearable placeholder="请选择建筑名称" @change="buildChange">
              <el-option v-for="item in buildList" :key="item.building_id" :label="item.name" :value="item.building_id" />
            </el-select>
          </el-form-item>
          <el-form-item label="分区名称:">
            <el-select v-model="listQuery.zone_id" clearable autocomplete="off" placeholder="请选择分区名称">
              <el-option v-for="item in partitionList" :key="item.zone_id" :label="item.name" :value="item.zone_id" />
            </el-select>
          </el-form-item>
          <el-form-item label="设备名称:">
            <el-input v-model="listQuery.mname" clearable placeholder="设备名称" autocomplete="off"/>
          </el-form-item>
          <el-form-item label="">
            <el-button v-permission="['v1/equips/index']" type="primary" size="small" icon="el-icon-search" @click="handleFilter">查询</el-button>
            <el-button v-permission="['v1/equips/create']" type="primary" size="small" icon="el-icon-plus" @click="handleAdd">添加设备</el-button>
            <el-button v-permission="['v1/equips/create']" type="primary" size="small" icon="el-icon-upload2" @click="importUser">批量添加设备</el-button>
            <el-button type="primary" size="small" icon="el-icon-download" @click="downloadTemplate">下载模板</el-button>
            <input id="upload" name="excelFile" class="hide" type="file" @change="upload">
          </el-form-item>
        </el-form>
      </div>
      <div v-if="isShow" class="data-list">
        <ul>
          <li v-for="item in listSelect" :key="item.index" @click="dataListFn(item.sid)">
            <div class="item-left">
              <img :src="item.imgUrl" alt="">
            </div>
            <div class="item-right">
              <span class="name">{{ item.sname }}</span>
              <span class="equipmentNum">设备数: {{ item.number }}</span>
            </div>
          </li>
        </ul>
        <div class="btn-left"><i class="el-icon-arrow-left" @click="leftFn"/></div>
        <div class="btn-right"><i class="el-icon-arrow-right" @click="rightFn"/></div>
      </div>
      <!-- 查询结果 -->
      <el-table v-loading="listLoading" :data="list" size="small" element-loading-text="正在查询中。。。" stripe border el-table-column fit highlight-current-row>
        <!--<el-table-column type="index" width="50" label="序号" />
        <el-table-column type="selection" width="50" label="选择" />-->
        <el-table-column align="center" label="设备名称" prop="mname">
          <template slot-scope="scope"><span class="hink" @click="handleEdit(scope.row)">{{ scope.row.mname }}</span></template>
        </el-table-column>
        <el-table-column align="center" label="位置" prop="position"/>
        <el-table-column align="center" label="单位" prop="corp_name"/>
        <el-table-column align="center" label="建筑群" prop="park_name"/>
        <el-table-column align="center" label="建筑" prop="building_name"/>
        <el-table-column align="center" label="楼层" prop="floor_name"/>
        <el-table-column align="center" label="分区" prop="zone_name"/>
        <el-table-column align="center" label="状态" width="100" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <!-- 启用 -->
            <el-switch v-permission="['v1/equips/enable']" v-model="scope.row.status" active-text="启用" inactive-text="禁用" active-color="#13910a" inactive-color="#f2473f" @click.native.prevent="iconQiyong(scope.$index, scope.row)"/>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.offset" @pagination="getList" />
      <formEquipment
        v-if="dialogVisible"
        :dialog-form="dialogForm"
        :dialog-type="dialogType"
        :dialog-visible.sync="dialogVisible"
        @getCancel="handleCancel"
        @closeBindWarnStandard="handleCloseBindWarnStandard"
        @submitForm="submitForm"/>
    </div>
  </div>
</template>
<script>
// equipmentExport
import { equipmentCountList, equipmentList, equipmentUpdate, equipmentExport } from '@/api/equipmentManage'
import { getSocialUnit } from '@/api/company'
import { parksList, buildList } from '@/api/park'
import { partitionList } from '@/api/partition'
import $ from 'jquery'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import formEquipment from './components/formEquipment.vue'
import TopMenu from '@/components/TopMenu' // 顶部菜单
import checkPermission from '@/utils/permission' // 权限判断函数
import internetData1 from '@/assets/internetData/internetData1.png'
import internetData2 from '@/assets/internetData/internetData2.png'
import internetData3 from '@/assets/internetData/internetData3.png'
import internetData4 from '@/assets/internetData/internetData4.png'
import internetData5 from '@/assets/internetData/internetData5.png'
import internetDataActive1 from '@/assets/internetData/internetDataActive1.png'
import internetDataActive2 from '@/assets/internetData/internetDataActive2.png'
import internetDataActive3 from '@/assets/internetData/internetDataActive3.png'
import internetDataActive4 from '@/assets/internetData/internetDataActive4.png'
import internetDataActive5 from '@/assets/internetData/internetDataActive5.png'
export default {
  name: 'Order',
  components: { Pagination, formEquipment, TopMenu },
  data() {
    return {
      corp_id: '',
      corp_name: '',
      internetData1,
      internetData2,
      internetData3,
      internetData4,
      internetData5,
      internetDataActive1,
      internetDataActive2,
      internetDataActive3,
      internetDataActive4,
      internetDataActive5,
      topDataLabel: {
        data: []
      },
      showTopMenu: false,
      listSelect: [],
      // 新增修改弹窗信息
      dialogForm: {
        corp_id: '',
        corp_name: '',
        park_name: '',
        building_name: '',
        floor_name: '',
        partition_name: '',
        position: '',
        sid: '',
        tid: '',
        mid: '',
        UID: '',
        upperLimit: '',
        lowerLimit: ''
      },
      dialogType: '添加',
      dialogVisible: false,
      isShow: false,
      // 表格信息
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        offset: 10,
        corp_id: '',
        corp_name: '',
        building_id: '',
        park_id: '',
        mname: '',
        zone_id: '',
        sid: ''
      },
      left: 40,
      leftNum: 0,
      companyType: {},
      companyList: [],
      parksList: [],
      buildList: [],
      partitionList: []
    }
  },
  created() {
    this.getCompanyList()
    // this.getParkList()
    // this.getBuildList()
    // this.getPartitionList()
    this.getCountList()
  },
  methods: {
    // 列表查询
    checkPermission,
    getList() {
      const that = this
      this.listLoading = true
      equipmentList(this.listQuery)
        .then(response => {
          const data = response.data
          if (data.code === 40000) {
            that.list = []
            response.data.data.data.forEach(function(value, index, array) {
              array[index].status = array[index].status !== 2
              that.list.push(array[index])
            })
            that.total = parseInt(response.data.data.total_count)
          }
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    // 获取设备数量
    getCountList() {
      const that = this
      equipmentCountList()
        .then(response => {
          const data = response.data
          if (data.code === 40000) {
            that.listSelect = []
            that.isShow = true
            response.data.data.forEach(function(value, index, array) {
              array[index].index = index
              // 1火灾自动报警系统2自动喷水系统3消防给水及消火栓系统4机械防排烟系统5智慧用电系统6其他系统
              if (array[index].sname.indexOf('火灾自动报警') > -1) {
                array[index].sname = array[index].sname.slice(0, array[index].sname.indexOf('系统'))
                array[index].imgUrl = internetDataActive1
              } else if (array[index].sname.indexOf('喷水灭火') > -1) {
                array[index].sname = array[index].sname.slice(0, array[index].sname.indexOf('系统'))
                array[index].imgUrl = internetDataActive2
              } else if (array[index].sname.indexOf('消防给水') > -1) {
                array[index].sname = array[index].sname.slice(0, array[index].sname.indexOf('系统'))
                array[index].imgUrl = internetDataActive3
              } else if (array[index].sname.indexOf('机械防排烟') > -1) {
                array[index].sname = array[index].sname.slice(0, array[index].sname.indexOf('系统'))
                array[index].imgUrl = internetDataActive4
              } else if (array[index].sname.indexOf('智慧用电') > -1) {
                array[index].sname = array[index].sname.slice(0, array[index].sname.indexOf('系统'))
                array[index].imgUrl = internetDataActive5
              }
              that.listSelect.push(array[index])
            })
            $('.data-list>ul').css({
              width: this.listSelect.length * 292 + 'px'
            })
            setTimeout(function() {
              that.listQuery.sid = that.listSelect[0].sid
              that.getList()
              $('.data-list>ul>li').eq(0).addClass('active')
              const dataLength = $('.data-list').width()
              const dataUlLength = $('.data-list>ul').width()
              if (dataLength > dataUlLength) {
                $('.el-icon-arrow-left').css({
                  color: '#B8AEAE'
                })
                $('.el-icon-arrow-right').css({
                  color: '#B8AEAE'
                })
              } else {
                $('.el-icon-arrow-left').css({
                  color: '#B8AEAE'
                })
                $('.el-icon-arrow-right').css({
                  color: '#409EFF'
                })
              }
            }, 300)
            $('.data-list>ul>li').click(function() {
              const index = $(this).index()
              $(this).addClass('active')
              $(this).siblings().removeClass('active')
              for (var i = 0; i < that.listSelect.length; i++) {
                if (index === i) {
                  that.listQuery.sid = that.listSelect[i].sid
                  that.listSelect[i].imgUrl = that['internetDataActive' + (index + 1)]
                } else {
                  that.listSelect[i].imgUrl = that['internetData' + (i + 1)]
                }
              }
              that.getList()
            })
          } else {
            that.getList()
            that.isShow = false
          }
        })
        .catch(() => {
        })
    },
    // 下载模板
    downloadTemplate() {
      var $form = $('<form method="GET"></form>')
      $form.attr('action', '/static/excels/equipmentTemplate.xlsx')
      $form.appendTo($('body'))
      $form.submit()
    },
    // 导入用户
    importUser() {
      $('#upload').click()
    },
    upload(e) {
      var file = e.target.files[0]
      const isImage = file.name.substr(file.name.length - 4) === 'xlsx'
      const that = this
      if (!isImage) {
        that.$message.warning('文件只能是 xlsx 格式!')
        return false
      }
      const image = new window.FormData()
      image.append('excelFile', file)
      var params = {
        fileType: 'file',
        data: image
      }
      equipmentExport(params)
        .then((res) => {
          const data = res.data
          if (data.code === 40000) {
            that.handleFilter()
            that.$message.success(data.message)
          } else {
            that.$message.warning(data.message)
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    // 查询
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 启用
    iconQiyong(index, data) {
      const is_forbidden = data.is_forbidden // 保存点击之后v-modeld的值(true，false)
      data.is_forbidden = !data.is_forbidden // 保持switch点击前的状态
      const that = this
      var flag = is_forbidden ? '启用' : '禁用'
      this.$confirm('此操作将' + flag + '该行数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        closeOnClickModal: false
      }).then(() => {
        var params = {
          device_id: data.device_id,
          status: is_forbidden ? 2 : 1
        }
        equipmentUpdate(params)
          .then((res) => {
            const data = res.data
            if (data.code === 40000) {
              that.$message.success(data.message)
              that.getList()
            } else {
              that.$message.warning(data.message)
              that.getList()
            }
          })
          .catch(() => {
            that.getList()
          })
      }).catch(() => {
        that.getList()
      })
    },
    // 新增用户
    handleAdd() {
      this.dialogType = '添加'
      this.dialogForm = {
        corp_id: '',
        park_id: '',
        building_id: '',
        floor_id: '',
        zone_id: '',
        park_name: '',
        building_name: '',
        floor_name: '',
        partition_name: '',
        position: '',
        sid: '',
        tid: '',
        mid: '',
        UID: '',
        upperLimit: '',
        lowerLimit: ''
      }
      this.dialogVisible = true
    },
    handleEdit(row) {
      var data = JSON.parse(JSON.stringify(row))
      this.dialogType = '修改'
      this.dialogForm = {
        device_id: data.device_id,
        corp_id: data.corp_id,
        park_id: data.park_id,
        building_id: data.building_id,
        floor_id: data.floor_id,
        zone_id: data.zone_id,
        park_name: data.park_name,
        building_name: data.building_name,
        floor_name: data.floor_name,
        partition_name: data.partition_name,
        position: data.position,
        sid: data.sid,
        tid: data.tid,
        mid: data.mid,
        UID: data.UID,
        upperLimit: data.upperLimit,
        lowerLimit: data.lowerLimit
      }
      this.dialogVisible = true
    },
    // 新增弹窗确认
    submitForm(formName) {
      this.setTimeoutFn()
      this.dialogVisible = false
      this.getList()
    },
    handleCancel() {
      this.dialogVisible = false
    },
    // 关闭弹窗
    handleCloseBindWarnStandard() {
      this.dialogVisible = false
    },
    // 顶部切换监听
    tabsChange(value) {
      const that = this
      this.companyList.forEach(function(data, index, array) {
        if (value === array[index].name) {
          that.listQuery.corp_id = array[index].corp_id
        }
      })
      this.getList()
    },
    // 点击右侧按钮
    rightFn() {
      const dataLength = $('.data-list').width()
      const dataUlLength = $('.data-list>ul').width()
      const max = ((this.listSelect.length - 3) * 250 - 40) * -1
      if (dataLength > dataUlLength) {
        $('.el-icon-arrow-left').css({
          color: '#B8AEAE'
        })
        $('.el-icon-arrow-right').css({
          color: '#B8AEAE'
        })
      } else if (this.left <= 40 && this.left > max) {
        this.leftNum--
        console.log(this.leftNum)
        this.left = 250 * this.leftNum + 40
        $('.data-list ul').animate({ left: this.left }, 800)
      }
      if (this.leftNum !== 0) {
        $('.el-icon-arrow-left').css({
          color: '#409EFF'
        })
      }
      if ((250 * this.leftNum - 250 + 40) < max) {
        $('.el-icon-arrow-left').css({
          color: '#409EFF'
        })
        $('.el-icon-arrow-right').css({
          color: '#B8AEAE'
        })
      }
    },
    // 点击左侧按钮
    leftFn() {
      const dataLength = $('.data-list').width()
      const dataUlLength = $('.data-list>ul').width()
      const max = ((this.listSelect.length - 3) * 250 - 40) * -1
      if (dataLength > dataUlLength) {
        $('.el-icon-arrow-left').css({
          color: '#B8AEAE'
        })
        $('.el-icon-arrow-right').css({
          color: '#B8AEAE'
        })
      } else if (this.left >= max && this.left < 40) {
        this.leftNum++
        console.log(this.leftNum)
        this.left = 250 * this.leftNum + 40
        $('.data-list ul').animate({ left: this.left }, 800)
        $('.el-icon-arrow-left').css({
          color: '#409EFF'
        })
        $('.el-icon-arrow-right').css({
          color: '#409EFF'
        })
      }
      if (this.leftNum === 0) {
        $('.el-icon-arrow-left').css({
          color: '#B8AEAE'
        })
      }
    },
    dataListFn(sid) {
      const that = this
      that.listQuery.sid = sid
      this.getList()
      // this.setTimeoutFn()
    },
    setTimeoutFn() {
      const that = this
      this.listLoading = true
      setTimeout(function() {
        that.listLoading = false
      }, 1000)
    },
    // 单位下拉框
    companyChange(value) {
      if (value !== '') {
        var select = {}
        this.companyList.forEach(function(data, index, array) {
          if (value === array[index].corp_id) {
            select = array[index]
          }
        })
        this.getParkList()
        if (this.topDataLabel.data.indexOf(select.name) === -1) {
          this.topDataLabel.data.push(select.name)
        }
      } else {
        this.partitionList = []
        this.listQuery.zone_id = ''
        this.buildList = []
        this.listQuery.building_id = ''
        this.parksList = []
        this.listQuery.park_id = ''
      }
    },
    parkChange(value) {
      if (value !== '') {
        this.getBuildList()
      } else {
        this.buildList = []
        this.listQuery.building_id = ''
        this.partitionList = []
        this.listQuery.zone_id = ''
      }
    },
    buildChange(value) {
      if (value !== '') {
        this.getPartitionList()
      } else {
        this.partitionList = []
        this.listQuery.zone_id = ''
      }
    },
    // 单位下拉框查询,
    getCompanyList() {
      const that = this
      getSocialUnit()
        .then(response => {
          if (response.data.code === 40000) {
            that.companyList = response.data.data
            that.showTopMenu = true
          } else if (response.data.code === 40003 && response.data.message === '权限验证失败!') {
            that.showTopMenu = false
          } else {
            that.$message.warning(response.data.message)
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    // 获取建筑群列表
    getParkList() {
      const that = this
      var query = {
        page: 1,
        offset: 1000,
        corp_id: that.listQuery.corp_id
      }
      parksList(query)
        .then(response => {
          if (response.data.code === 40000) {
            that.parksList = []
            response.data.data.data.forEach(function(value, index, array) {
              array[index].index = index
              that.parksList.push(array[index])
            })
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    // 获取建筑
    getBuildList() {
      const that = this
      var listQuery = {
        page: 1,
        offset: 1000
      }
      buildList(listQuery)
        .then(response => {
          if (response.data.code === 40000) {
            that.buildList = []
            response.data.data.data.forEach(function(value, index, array) {
              array[index].index = index
              that.buildList.push(array[index])
            })
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    // 获取分区
    getPartitionList() {
      const that = this
      var listQuery = {
        page: 1,
        offset: 1000,
        building_id: that.dialogForm.building_id
      }
      partitionList(listQuery)
        .then(response => {
          if (response.data.code === 40000) {
            that.partitionList = []
            response.data.data.data.forEach(function(value, index, array) {
              array[index].index = index
              that.partitionList.push(array[index])
            })
          }
        })
        .catch((err) => {
          console.log(err)
        })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.hide{
  display: none;
}
.more{
  color: #409EFF;
}
.data-list{
  position: relative;
  padding: 0 20px;
  height: 86px;
  overflow: hidden;
  margin-bottom: 40px;
  margin-top: 20px;
  > div{
    width: 40px;
    height: 86px;
    line-height: 86px;
    position: absolute;
    top: 0px;
    color: #B4AEAE;
    font-size: 26px;
    background: #FFFFFF;
    i{
      cursor: pointer;
    }
  }
  .btn-left{
    left: 0;
  }
  .btn-right{
    right: 0;
  }
  > ul{
    padding: 0;
    margin: 0;
    overflow: auto;
    width: 1350px;
    height: 86px;
    position: absolute;
    left: 40px;
    top: 0;
    > li{
      width: 242px;
      height: 86px;
      float: left;
      font-size: 14px;
      border: 1px solid #AAAAAA;
      border-radius: 5px;
      padding-right: 10px;
      box-sizing: border-box;
      margin-right: 8px;
      cursor: pointer;
      .item-left{
        width: 70px;
        float: left;
        text-align: center;
        padding-top: 12px;
        .svg-icon{
          font-size: 50px;
          margin-bottom: 10px;
          color: #AAAAAA;
        }
        span{
          display: block;
          width: 120px;
          height: 22px;
          line-height: 22px;
        }
      }
      .item-right{
        width: 160px;
        float: left;
        padding-left: 20px;
        padding-top: 4px;
        .name{
          width: 100%;
          height: 36px;
          line-height: 36px;
          display: block;
          font-size: 16px;
          font-weight: 600;
          color: #AAAAAA;
        }
        .equipmentNum{
          width: 100%;
          height: 36px;
          line-height: 36px;
          display: block;
          font-size: 12px;
          font-weight: 600;
          color: #AAAAAA;
        }
      }
    }
    > li.active{
      border-color: #4BCBFD;
      .svg-icon{
        color: #F32F32;
      }
      .name{
        color: #4BCBFD;
      }
      .equipmentNum{
        color: #4BCBFD;
      }
    }
    > li:last-child{
      margin-right: 0px;
    }
  }
}
</style>
